package com.zxy.controller;


import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zxy.common.ResultData;
import com.zxy.dto.TrafficInfo;
import com.zxy.entity.Traffic;
import com.zxy.mapper.TrafficMapper;
import com.zxy.service.ITrafficService;
import lombok.AllArgsConstructor;
import org.apache.ibatis.annotations.Delete;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Zxy
 * @since 2021-05-11
 */
@RestController
@RequestMapping("/traffic")
@AllArgsConstructor
public class TrafficController {

    private final ITrafficService trafficService;
    private final TrafficMapper trafficMapper;

    // 新增发布交通信息
    @PostMapping("/public")
    public ResultData publicTraffic(@RequestBody Traffic traffic) {
        traffic.setTime(DateUtil.now());
        boolean save = trafficService.save(traffic);
        if (save) {
            return ResultData.success(save);
        } else {
            return ResultData.fail("保存失败，请联系网站管理员");
        }
    }

    // 全部交通指引信息
    @GetMapping("/findlist")
    public ResultData findList(){
        List<TrafficInfo> trafficInfos = trafficMapper.findList();
        return ResultData.success(trafficInfos);
    }

    // 根据id删除发布的新闻
    @DeleteMapping("/deletebyid")
    public ResultData deleteById(Integer id){
        QueryWrapper<Traffic> wrapper = new QueryWrapper<>();
        wrapper.eq("id",id);
        trafficService.remove(wrapper);
        return ResultData.success("删除成功");
    }
}
